package com.wxxymakers.grademark.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Random;

/**
 * @AUTHOR soft
 * @DATE 2018/11/17 19:38
 * @DESCRIBE 随机验证码
 */
public class RandomVerify {
    // 验证码长度
    private final static int verLength = 4;

    // Session Key
    public static String S_KEY = "RandomVerify_Key";

    public static String create(OutputStream out) {
        return create(130, 40, "png", out);
    }

    /**
     * 以字符串形式返回生成的验证码，同时输出一个图片
     * @param width 图片的宽度
     * @param height 图片的高度
     * @param imgType 图片的类型
     * @param output 图片的输出流(图片将输出到这个流中)
     * @return 返回所生成的验证码(字符串)
     */
    public static String create(final int width, final int height,
                                final String imgType, OutputStream output) {
        StringBuilder sb = new StringBuilder();
        Random random = new Random();

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphic = image.getGraphics();

        graphic.setColor(Color.decode("#F8F8F8"));
        graphic.fillRect(0, 0, width, height);

        Color[] colors = new Color[]{Color.BLACK, Color.decode("#6E2F44"), Color.decode("#397C5F"),
                Color.decode("#9D982D"), Color.decode("#3C6894"), Color.decode("#379195")};
        // 在 "画板"上生成干扰线条 ( 50 是线条个数)
        for (int i = 0; i < 50; i++) {
            graphic.setColor(colors[random.nextInt(colors.length)]);
            final int x = random.nextInt(width);
            final int y = random.nextInt(height);
            final int w = random.nextInt(20);
            final int h = random.nextInt(20);
            final int signA = random.nextBoolean() ? 1 : -1;
            final int signB = random.nextBoolean() ? 1 : -1;
            graphic.drawLine(x, y, x + w * signA, y + h * signB);
        }

        // 在 "画板"上绘制字母
        graphic.setFont(new Font("Comic Sans MS", Font.BOLD, 30));
        for (int i = 0; i < verLength; i++) {
            final int temp = random.nextInt(26) + 97;
            String s = String.valueOf((char) temp).toUpperCase();
            sb.append(s);
            graphic.setColor(colors[random.nextInt(colors.length)]);
            graphic.drawString(s, i * (width / verLength) + 2, height - (height / verLength / 2));
        }
        graphic.dispose();
        try {
            ImageIO.write(image, imgType, output);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sb.toString();
    }
}
